import datetime

from flask_restful import Resource, reqparse, fields, marshal
from sqlalchemy import or_

from App.apis.api_constant import HTTP_OK
from App.apis.movie_user.utils import login_required
from App.models.cinema_admin.cinema_address_model import CinemaAddress
from App.models.cinema_admin.cinema_hall_model import Hall
from App.models.cinema_admin.cinema_hall_movie_model import HallMovie
from App.models.movie_user.movie_order_model import MovieOrder, ORDER_STATUS_GET, ORDER_STATUS_PAID_BUT_NOT_GET, \
    ORDER_STATUS_NOT_PAY

parse = reqparse.RequestParser()
parse.add_argument("movie_id")
parse.add_argument("address_id")
parse.add_argument("district")


hall_movie_fields = {
    "id":fields.Integer,
    "h_movie_id":fields.Integer,
    "h_hall_id":fields.Integer,
    "h_time":fields.DateTime,
}

multi_hall_movie_fields = {
    "msg":fields.String,
    "status":fields.Integer,
    "data":fields.List(fields.Nested(hall_movie_fields))
}

class UserMovieHallsResource(Resource):

    def post(self):
        args = parse.parse_args()
        movie_id = args.get("movie_id")
        address_id = args.get("address_id")
        district = args.get("district")
        # 地区和id都得一样的双重过滤查询
        cinema_address = CinemaAddress.query.filter(CinemaAddress.district == district).filter(CinemaAddress.id == address_id).first()

        halls = Hall.query.filter(Hall.h_address_id == cinema_address.id).all()
        all_hall_movies = []
        for hall in halls:
            hall_movies = HallMovie.query.filter(HallMovie.h_hall_id == hall.id).filter(HallMovie.h_movie_id == movie_id).all()
            if hall_movies:
                all_hall_movies.append(hall_movies)

        data = {
            "msg":"ok",
            "status":HTTP_OK,
            "data":all_hall_movies
        }

        return marshal(data , multi_hall_movie_fields)

hall_fields = {
    "h_num":fields.Integer,
    "h_seats":fields.String,
    "address_id":fields.Integer(attribute="h_address_id")
}

class UserMovieHallResource(Resource):
    @login_required
    def get(self , id):
        hall_movie = HallMovie.query.get(id)
        hall = Hall.query.get(hall_movie.h_hall_id)

        movie_orders_paid = MovieOrder.query.filter(MovieOrder.o_hall_movie_id == id).filter(or_(MovieOrder.o_status == ORDER_STATUS_PAID_BUT_NOT_GET , MovieOrder.o_status == ORDER_STATUS_GET)).all()
        movie_orders_lock = MovieOrder.query.filter(MovieOrder.o_hall_movie_id == id).filter(MovieOrder.o_status == ORDER_STATUS_NOT_PAY).filter(MovieOrder.o_time > datetime.datetime.now()).all()

        seats = []      # 里面存的是已经卖了的和已经锁单了的
        for movie_orders in movie_orders_paid:
            sold_seats = movie_orders.o_seats.split("#")
            # seats.append(sold_seats)      # 这样加，列表里都是列表，转set就转不回list。不能list不可哈希的元素
            seats += sold_seats     # 这样加，列表里面就不会有列表
        for movie_orders in movie_orders_lock:
            sold_seats = movie_orders.o_seats.split("#")
            # seats.append(sold_seats)
            seats += sold_seats

        all_seats = hall.h_seats.split("#")
        can_pay = list(set(all_seats) - set(seats))
        hall.h_seats = "#".join(can_pay)      # 这里没有save更新，就不会更新到数据库，只是返回到客户端

        # print(all_seats ,"====" , seats , "====" , can_pay , "====")

        data = {
            "msg":"ok",
            "status":HTTP_OK,
            "data":marshal(hall , hall_fields)
        }
        return data







'''
ORDER_STATUS_NOT_PAY = 0
ORDER_STATUS_PAID_BUT_NOT_GET = 1
ORDER_STATUS_GET = 2
ORDER_STATUS_TIMEOUT = 3
'''